<!DOCTYPE html>
<html class="x-admin-sm">
<!--添加管理员页面-->
<head>
    <meta charset="UTF-8">
    <title>气清</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <script type="text/javascript" src="./js/cookie.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>

<body>
<div class="x-body">
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="leaveType" class="layui-form-label">
                <span class="x-red">*</span>请假类型
            </label>
            <div class="layui-input-inline">
                <select lay-verify="leaveType" id="leaveType" name="leaveType">
                    <option value=""></option>
                    <option value="事假">事假</option>
                    <option value="探亲假">探亲假</option>
                    <option value="丧假">丧假</option>
                    <option value="产假">产假</option>
                    <option value="婚假">婚假</option>
                    <option value="病假">病假</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item ">
            <label for="start" class="layui-form-label">
                <span class="x-red">*</span>开始时间
            </label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="start" id="start" placeholder="yyyy-MM-dd"
                       lay-verify="startTime" autocomplete="off">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>
        <div class="layui-form-item ">
            <label for="end" class="layui-form-label">
                <span class="x-red">*</span>结束时间
            </label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="end" id="end" placeholder="yyyy-MM-dd" lay-verify="endTime"
                       autocomplete="off">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>
        <div class="layui-form-item ">
            <label for="reason" class="layui-form-label">
                <span class="x-red">*</span>请假原因
            </label>
            <div class="layui-input-inline">
                <textarea placeholder="请输入内容" class="layui-textarea" id="reason" name="reason" required=""
                          lay-verify="reason"></textarea>
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>
        <div class="layui-form-item " style="left: 100px">
            <label for="reason" class="layui-form-label">
            </label>
            <button class="layui-btn" lay-filter="edit" lay-submit="">
                修改申请
            </button>
        </div>
    </form>
</div>
<script>
    var leaveApplyid;

    init();

    function init() {
        var url = window.location.href;
        var id = url.split('?id=')[1];
        leaveApplyid = id;
        $.ajax({
            async: false,
            url: '/apply-list/show',
            data: {"id": id},
            dataType: 'json',
            success: function (result) {
                console.log(result);
                var leaveApply = result.data;
                $('#reason').val(leaveApply.reason);
                $('#start').val(leaveApply.start);
                $('#end').val(leaveApply.end);
                document.getElementsByName("leaveType")[0].value = leaveApply.leaveType;

            }
        });
    }

    layui.use(['form', 'layer', 'laydate'], function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;
        var laydate = layui.laydate;
        //常规用法
        laydate.render({
            elem: '#start'
        });
        laydate.render({
            elem: '#end'
        });
        form.verify({
            leaveType: function (value) {
                if (value == '') {
                    return '请填写请假类型'
                }
            }
            ,
            reason: function (value) {
                if (value == '') {
                    return '请填写请假原因'
                }
            }
            ,
            startTime: function (value, item) {
                if (value == '') {
                    return '请填写请假时间'
                }
            }
            ,
            endTime: function (value, item) {
                if (value == '') {
                    return '请填写请假时间'
                } else {
                    if (checkTime() == false) {
                        return '结束时间应当晚于开始时间'
                    }
                }
            }
        })

        function checkTime() {
            if ($('#start').val() < $('#end').val()) {
                return true;
            }
            return false;
        }

        //监听提交
        form.on('submit(edit)', function (data) {
            console.log(data.field);
            var leaveApply =data.field
            leaveApply.id =leaveApplyid;
            $.ajax({
                url: '/apply-list/edit',
                dataType: "json",
                type: "post",
                data: leaveApply,
                success: function (result) {
                    if (result.status == 200) {
                        layer.alert("修改成功", {icon: 6}, function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                            // 可以对父窗口进行刷新
                            x_admin_father_reload();
                        })
                    } else {
                        layer.alert("修改失败", {icon: 6})
                        layer.alert(result.data)
                    }
                }
            })

            return false;
        });


    });
</script>

</body>

</html>